/*
 * 微信小程序图表工具类
 * 简化版本，仅支持饼图
 */

function WxCharts(config) {
  this.config = config;
  this.canvas = wx.createCanvasContext(config.canvasId);
  this.initChart();
}

WxCharts.prototype.initChart = function() {
  const series = this.config.series;
  const width = this.config.width;
  const height = this.config.height;
  
  // 计算总数
  let total = 0;
  series.forEach(item => {
    total += item.data;
  });
  
  // 绘制饼图
  let startAngle = 0;
  const radius = Math.min(width, height) / 2 * 0.7;
  const centerX = width / 2;
  const centerY = height / 2;
  
  series.forEach(item => {
    const percent = item.data / total;
    const endAngle = startAngle + percent * 2 * Math.PI;
    
    // 绘制扇形
    this.canvas.beginPath();
    this.canvas.moveTo(centerX, centerY);
    this.canvas.arc(centerX, centerY, radius, startAngle, endAngle);
    this.canvas.setFillStyle(item.color);
    this.canvas.fill();
    
    startAngle = endAngle;
  });
  
  // 绘制中心圆
  this.canvas.beginPath();
  this.canvas.arc(centerX, centerY, radius * 0.6, 0, 2 * Math.PI);
  this.canvas.setFillStyle('#ffffff');
  this.canvas.fill();
  
  // 绘制完成
  this.canvas.draw();
};

module.exports = WxCharts;